package servlets;

import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;

//缺省适配器设计模式
public abstract class GenericServlet implements Servlet, ServletConfig {

    private ServletConfig config;

    //模板方法设计模式
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

        this.config = servletConfig;

        //调用无参的init()方法
        this.init();
        System.out.println("=============================");

    }


    //该无参init()方法，就是要让子类来重写的
    public void init() {

    }

    @Override
    public ServletConfig getServletConfig() {
        return config;
    }


    //抽象方法，专门让子类来实现
    @Override
    public abstract void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException;

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }

    @Override
    public String getServletName() {
        return config.getServletName();
    }

    @Override
    public ServletContext getServletContext() {
        return config.getServletContext();
    }

    @Override
    public String getInitParameter(String s) {
        return config.getInitParameter(s);
    }

    @Override
    public Enumeration getInitParameterNames() {
        return config.getInitParameterNames();
    }
}
